<?php
/**
 * 公司
 */
require_once("../global.inc.php");

class SysCompanydeptClass extends SystemFrameWork{
	/**
	 * 公司对象
	 *
	 * @var obj
	 */
	var $obj_companydept;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	
	function main(){
		/**
		 * 客户公司
		 */
		if (!is_object($this->obj_companydept)){
			require_once ("companydept.class.php");
			$this->obj_companydept = new CompanydeptClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		
		switch($this->_input['action']){
			case "ajax_list":
				$this->_ajax_list();
				break;
            case 'list':
                $this->_list();
                break;
			case "add":
				$this->_add();
				break;
			case "save":
				$this->_save();
				break;
			case "modi":
				$this->_modi();
				break;
			case "update":
				$this->_update();
				break;
			case "del":
				$this->_del();
				break;
			case "view":
				$this->_view();
				break;
			default:
				$this->_list();
		}
	}
	/**
	 * 列表
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * ajax类表
	 */
	function _ajax_list(){
		$type = intval($this->_input['t']);
		//搜索条件
		$condition = " where companydept.status='1' order by companydept.id desc ";

		//信息列表
		$this->obj_page->pagebarnum(10);
		$this->obj_page->nowindex = $this->_input['curpage']?$this->_input['curpage']:1;
		$companydept_array = $this->obj_companydept->listCompanydept($condition,$this->obj_page);
		
		//对时间进行格式化
		if (is_array($companydept_array)){
			for ($i=0;$i<count($companydept_array);$i++){
				$companydept_array[$i]['create_time'] = @date("Y-m-d",$companydept_array[$i]['create_time']);
			}
		}

		$page_list = $this->obj_page->show(2);
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->obj_page->nowindex);
		$this->output('companydept_array',$companydept_array);
		$this->output('page_list',$page_list);
		$this->showpage('companydept.list');
	}
	/**
	 * 信息添加
	 */
	function _add(){
		$this->showpage('companydept.add');
	}
	/**
	 * 信息添加
	 */
	function _save(){
		$value_array = array();
		$value_array['company_id'] = $this->_input["hidCompanyId"];  //客户ID
		$value_array['realname'] = trim($this->_input["realname"]);  //名称
		$value_array['phone'] = trim($this->_input["phone"]);  //电话
		$value_array['address'] = trim($this->_input["address"]);  //地址
		$value_array['post'] = trim($this->_input["post"]);  //邮编
		$value_array['create_id'] = $_SESSION['s_admin']['id'];  //创建者ID
		$value_array['create_time'] = time();  //创建时间
		$value_array['status'] = 1;  //状态
		$result = $this->obj_companydept->addCompanydept($value_array);
			
		if ($result != true){
			$this->redirectPath('error','','添加部门失败！',1);
		}else {
			//记录操作日志
			SystemPowerClass::addSysLog('客户->部门管理->添加部门');
			$this->redirectPath('error','./companydept.manage.php?action=list','添加部门成功！',1);
		}
		
	}
	
	/**
	 * 修改信息
	 */
	function _modi(){
		$id = $this->_input["id"];
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$id, "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$companydept_array = $this->obj_companydept->getCompanydept($id, 'more');
			
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('companydept_array',$companydept_array);
			$this->showpage('companydept.modi');
		}
	}
	
	/**
	 * 保存修改
	 */
	function _update(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsIdIsEmpty']),
//			array("input"=>$this->_input["txttitle"], "require"=>"true", "message"=>$this->_lang['errSysNNewsNameIsEmpty']),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong']),		
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			$value_array = array();
			$value_array['id'] = $this->_input["id"];
			$value_array['company_id'] = $this->_input["hidCompanyId"];  //客户ID
			$value_array['realname'] = trim($this->_input["realname"]);  //名称
			$value_array['phone'] = trim($this->_input["phone"]);  //电话
			$value_array['address'] = trim($this->_input["address"]);  //地址
			$value_array['post'] = trim($this->_input["post"]);  //邮编
			$result = $this->obj_companydept->updateCompanydept($value_array);
			
			if ($result != true){
				$this->redirectPath('error','','更新部门失败！',1);
			}else {
				//记录操作日志
				SystemPowerClass::addSysLog('客户->部门管理->更新部门');
				$this->redirectPath('error','./companydept.manage.php?action=list','更新部门成功！',1);
			}
//		}
	}
	
	/**
	 * 修改信息
	 */
	function _view(){
	    $id = $this->_input["id"];
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$id, "require"=>"true","validator"=>"Number", "message"=>'参数错误！'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}else {
			//取信息
			$companydept_array = $this->obj_companydept->getCompanydept($id, 'more');
			
			/**
			 * 页面输出
			 */
			$this->output("curpage", $this->_input['curpage']);
			$this->output('companydept_array',$companydept_array);
			$this->showpage('companydept.view');
		}
	}
	/**
	 * 删除信息
	 */
	function _del(){
		if (is_array($this->_input['id'])){
			foreach ($this->_input['id'] as $v) {
				$this->obj_companydept->delCompanydept($v);
			}
			//记录操作日志
			SystemPowerClass::addSysLog('客户->部门管理->删除部门');
			$this->redirectPath('error','','删除部门成功！',1);
		}else {
			$this->redirectPath('error', '', '删除部门失败！', 1);
		}
	}

}
$companydept = new SysCompanydeptClass();
$companydept->main();
unset($companydept);
?>